package store.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 商品
 */
public class Goods {

    /**
     * 商品编号
     */
    private int goodNo;

    /**
     * 商品名称
     */
    private String goodsName;

    /**
     * 成本
     */
    private double cost;

    /**
     * 售价
     */
    private double price;

    /**
     * 数量
     */
    private int count;

    /**
     * 商品分类
     */
    //private Category category;
    private int category_no;
    private Date createTime;

    public Goods() {
    }

    public Goods(int no) {
        this.goodNo = no;
    }

    public Goods(int goodNo, String goodsName, double cost, double price, int count, int category_no, Date createTime) {
        this.goodNo = goodNo;
        this.goodsName = goodsName;
        this.cost = cost;
        this.price = price;
        this.count = count;
        this.category_no = category_no;
        this.createTime = createTime;
    }

    public static class GoodsRecover implements Recover<Goods> {

        @Override
        public List<Goods> recover(ResultSet set) throws SQLException {
            List<Goods> goodsList = new ArrayList<>();
            while (set.next()) {
                goodsList.add(new Goods(set.getInt("goods_no"),
                        set.getString("goods_name"),
                        set.getDouble("cost"),
                        set.getDouble("price"),
                        set.getInt("count"),
                        set.getInt("category_no"),
                        set.getDate("create_time")));
            }
            return goodsList;
        }
    }

    public int getGoodNo() {
        return goodNo;
    }

    public void setGoodNo(int goodNo) {
        this.goodNo = goodNo;
    }

    public int getCategory_no() {
        return category_no;
    }

    public void setCategory_no(int category_no) {
        this.category_no = category_no;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "goodNo=" + goodNo +
                ", goodsName='" + goodsName + '\'' +
                ", cost=" + cost +
                ", price=" + price +
                ", count=" + count +
                ", category_no=" + category_no +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Goods goods = (Goods) o;
        return goodNo == goods.goodNo && Double.compare(goods.cost, cost) == 0 && Double.compare(goods.price, price) == 0 && count == goods.count && category_no == goods.category_no && Objects.equals(goodsName, goods.goodsName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(goodNo);
    }
}
